package com.shopp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopp.auto.JSONAuthentication;
import com.shopp.entity.ShFood;
import com.shopp.entity.ShUser;
import com.shopp.mapper.ShFoodMapper;
import com.shopp.response.Result;
import com.shopp.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shopp
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/sh-food")
public class ShFoodController extends JSONAuthentication {
    @Autowired
    ShFoodMapper shFoodMapper;
    @ApiOperation(value = "添加商品")
    @PostMapping("/add")
    public void addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, @RequestParam("fname") String fname, @RequestParam("content") String content, @RequestParam("price") float price) throws Exception {
        ShFood shFood=new ShFood();
        shFood.setFname(fname);
        shFood.setContent(content);
        shFood.setPrice(price);
        byte[] bytes = file.getBytes();
        String imageFileName = file.getOriginalFilename();
        String fileName = UpPhotoNameUtils.getPhotoName("img", imageFileName);Path path = Paths.get("D:\\自己的项目\\订餐系统\\order\\src\\assets\\picture\\" + fileName);
        Files.write(path, bytes);//写入文件
        String avatar_url = fileName;
        shFood.setPicture(avatar_url);
            response.reset();
            if (shFoodMapper.insert(shFood) > 0) {
                Result ok = Result.ok().message("添加成功");
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
    }
    @ApiOperation(value = "删除商品")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = shFoodMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改商品名字")
    @PostMapping("/updFoodName")
    public void updFoodName(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id, @RequestParam("fname") String fname) throws Exception {
            ShFood shFood=shFoodMapper.selectById(id);
            shFood.setFname(fname);
        if (shFoodMapper.updateById(shFood) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改商品介绍")
    @PostMapping("/updFoodContent")
    public void updFoodContent(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id, @RequestParam("content") String content) throws Exception {
        ShFood shFood=shFoodMapper.selectById(id);
        shFood.setContent(content);
        if (shFoodMapper.updateById(shFood) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改商品价格")
    @PostMapping("/updFoodPrice")
    public void updFoodPrice(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id, @RequestParam("price") float price) throws Exception {
        ShFood shFood=shFoodMapper.selectById(id);
        shFood.setPrice(price);
        if (shFoodMapper.updateById(shFood) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看全部商品")
    @PostMapping("/LookAllFood")
    public void LookAllFood(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int  size, @RequestParam("current") int current) throws Exception {
        Page<ShFood> page=new Page<>(current,size);
        Page<ShFood> pages=shFoodMapper.selectPage(page,null);
        List<ShFood> records = pages.getRecords();
        int total=shFoodMapper.selectCount(null);
        if (records.size() > 0) {
            Result ok = Result.ok().data("foods",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无商品");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据商品姓名查询商品")
    @PostMapping("/LookAllFoodByName")
    public void LookAllFoodByName(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int  size, @RequestParam("current") int current,@RequestParam("fname") String fname) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("fname",fname);
        Page<ShFood> page=new Page<>(current,size);
        Page<ShFood> pages=shFoodMapper.selectPage(page,wrapper);
        List<ShFood> records = pages.getRecords();
        int total =shFoodMapper.selectCount(wrapper);
        if (records.size() > 0) {
            Result ok = Result.ok().data("foods",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无商品,换个名字查吧");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据商品价格查询")
    @PostMapping("/LookAllFoodByPrice")
    public void LookAllFoodByPrice(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int  size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("price");
        Page<ShFood> page=new Page<>(current,size);
        Page<ShFood> pages=shFoodMapper.selectPage(page,wrapper);
        List<ShFood> records = pages.getRecords();
        int total =shFoodMapper.selectCount(wrapper);
        if (records.size() > 0) {
            Result ok = Result.ok().data("foods",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无商品,换个名字查吧");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据商品id查询商品")
    @PostMapping("/LookFoodById")
    public void LookFoodById(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id) throws Exception {
        ShFood shFood = shFoodMapper.selectById(id);

        if (shFood!=null) {
            Result ok = Result.ok().data("food",shFood);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无商品");
            this.WriteJSON(request, response, error);
        }
    }
}

